package com.wsh.sms.controller;

import com.wsh.sms.entity.Employee;
import com.wsh.sms.service.EmployeeService;
import com.wsh.sms.utils.AccountExist;
import com.wsh.sms.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wsh
 */
@RestController
@RequestMapping("/employee")
@CrossOrigin
public class EmployeeController {

    private final EmployeeService employeeService;

    @Autowired
    public EmployeeController(EmployeeService employeeService){
        this.employeeService = employeeService;
    }

    @RequestMapping(value = "/get-all-employee")
    public Result getAllEmployee(){
        List<Employee> allEmployees = employeeService.getAllEmployee();
        if(allEmployees == null){
            return new Result<>(200,"not-found","没有任何职工信息");
        }
        else {
            return new Result<>(200,"success",allEmployees);
        }
    }

    @RequestMapping(value = "/get-one-employee")
    public Employee getOneEmployee(@RequestParam(name = "number") String number){
        return employeeService.getOneEmployee(number);
    }

    @RequestMapping(value = "/delete-one-employee")
    public Result deleteOneEmployee(@RequestParam(name = "number") String number){
        int result = 0;
        try{
            result = employeeService.deleteOneEmployee(number);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (result >= 1){
            return new Result<>(200,"success","删除成功");
        }
        else {
            return new Result<>(200,"fail","删除失败");
        }
    }

    @RequestMapping(value = "/update-employee")
    public Result updateEmployee(
            @RequestParam(name = "number") String number,
            @RequestParam(name = "name") String name,
            @RequestParam(name = "sex") String  sex,
            @RequestParam(name = "age") int age,
            @RequestParam(name = "yearIn") String yearIn,
            @RequestParam(name = "old") String old
    ){
        int result = 0;
        try{
            result = employeeService.updateEmployee(number,name,sex,age,yearIn,old);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (result >= 1){
            return new Result<>(200,"success","更新成功");
        }
        else {
            return new Result<>(200,"fail","更新失败");
        }
    }

    @RequestMapping(value = "/add-employee")
    public Result addEmployee(
            @RequestParam(name = "number") String number,
            @RequestParam(name = "name") String name,
            @RequestParam(name = "sex") String  sex,
            @RequestParam(name = "age") int age,
            @RequestParam(name = "yearIn") String yearIn
    ){
        int result = 0;
        try{
            result = employeeService.addEmployee(number,name,sex,age,yearIn);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (result >= 1){
            return new Result<>(200,"success","添加成功");
        }
        else {
            return new Result<>(200,"fail","添加失败");
        }
    }

    @RequestMapping(value = "/existNumber")
    public Result existAccount(@RequestParam(name = "number") String number){
        String data = employeeService.existNumber(number);
        if (AccountExist.EXIST.getExist().equals(data)){
            return new Result<>(200,"success","工号存在");
        }
        else {
            return new Result<>(200,"fail","工号不存在");
        }
    }

}
